package com.wake.mycomposebook.ui.home.layouts

import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity
import com.google.android.material.datepicker.MaterialDatePicker
import com.google.android.material.timepicker.MaterialTimePicker
import com.wake.data.AlbumsDataProvider

/**
 *@author created by WA03675
 *@date  2022-04-21 10:19
 *@des
 */

data class DialogState(var show: Boolean, var type: String)

@Composable
fun MyDialog() {
    MyDialogList()
}

@Composable
fun MyDialogList() {
    val types = listOf(
        "Plan Message Dialog",
        "Title Dialog",
        "Dialog with Vertical buttons",
        "Dialog with Image",
        "Long Dialog",
        "Extra Rounded Dialog",
        "Date Picker Dialog",
        "Time Picker Dialog"
    )

    var dialogState = remember {
        mutableStateOf(DialogState(false, ""))
    }

    if (dialogState.value.show) {
        showDialog(dialogState.value) {
            dialogState.value = dialogState.value.copy(show = false)
        }
    }


    LazyColumn {
        items(types, key = { item -> return@items item }) {
            MyDialogListItem(msg = it, dialogState)
        }
    }
}

@Composable
fun MyDialogListItem(msg: String, dialogState: MutableState<DialogState>) {
    Button(
        onClick = {
            dialogState.value = dialogState.value.copy(show = true, type = msg)
        },
        Modifier
            .padding(10.dp, 10.dp)
            .fillMaxWidth()
    ) {
        Text(text = msg, color = MaterialTheme.colorScheme.background)
    }
}

@Composable
fun showDialog(dialogState: DialogState, click: () -> Unit) {
    when (dialogState.type) {
        "Plan Message Dialog" -> {
            MyPlanMsgDialog(click)
        }
        "Title Dialog" -> {
            MyTitleDialog(click = click)
        }
        "Dialog with Vertical buttons" -> {
            MyVBDialog(click)
        }
        "Dialog with Image" -> {
            MyImgDialog(click)
        }
        "Long Dialog" -> {}
        "Extra Rounded Dialog" -> {}
        "Date Picker Dialog" -> {
            MyDateDialog(click)
        }
        "Time Picker Dialog" -> {
            MyTimeDialog(click)
        }
        else -> {}
    }
}

@Composable
fun MyTimeDialog(click: () -> Unit) {
    val context = LocalContext.current
    (context as FragmentActivity).supportFragmentManager?.let {
        val builder = MaterialTimePicker.Builder().build()
        builder.addOnPositiveButtonClickListener{

        }

        builder.addOnDismissListener {

        }
        builder.show(it,"TimePicker")

    }
}

@Composable
fun MyDateDialog(click: () -> Unit) {
    val context = LocalContext.current
    (context as FragmentActivity)?.supportFragmentManager?.let {
        val builder = MaterialDatePicker.Builder.datePicker().build()
        builder.addOnPositiveButtonClickListener { selectdDate ->

        }
        builder.addOnDismissListener { click.invoke() }

        builder.show(it, "DatePicker")
    }
}


@Composable
fun MyPlanMsgDialog(click: () -> Unit) {
    AlertDialog(
        onDismissRequest = click,
        text = {
            MyDialogTitle()
        },
        confirmButton = {
            Text(
                text = "Ok",
                color = MaterialTheme.colorScheme.primary,
                modifier = Modifier.clickable { click.invoke() }
            )
        }
    )
}


@Composable
fun MyTitleDialog(click: () -> Unit) {
    AlertDialog(
        onDismissRequest = click,
        title = {
            Text(text = "title")
        },
        text = {
            MyDialogContent()
        },
        confirmButton = {
            Text(
                text = "Cancel",
                color = MaterialTheme.colorScheme.onPrimary,
                modifier = Modifier.clickable { click.invoke() }
            )
            Text(
                text = "Ok",
                color = MaterialTheme.colorScheme.primary,
                modifier = Modifier.clickable { click.invoke() }
            )
        },
    )
}


@Composable
fun MyVBDialog(click: () -> Unit) {
    AlertDialog(
        onDismissRequest = click,
        title = {
            Text(text = "title")
        },
        text = {
            MyDialogContent()
        },
        confirmButton = {
            Button(
                onClick = click,
                colors = ButtonDefaults.buttonColors(containerColor = MaterialTheme.colorScheme.background),
                shape = RoundedCornerShape(30.dp)
            ) {

                Text(
                    text = "Cancel",
                    color = MaterialTheme.colorScheme.onPrimary,
                )
            }

            Button(onClick = click) {
                Text(
                    text = "Ok",
                    color = MaterialTheme.colorScheme.onBackground,
                )
            }


        },
    )
}


@Composable
fun MyImgDialog(click: () -> Unit) {
    AlertDialog(
        onDismissRequest = click,
        icon = {
            Image(painter = painterResource(id = AlbumsDataProvider.album.imageId), contentDescription = null)
        },
        confirmButton = {
            Text(
                text = "Ok",
                color = MaterialTheme.colorScheme.primary,
                modifier = Modifier.clickable { click.invoke() }
            )
        }
    )

}


@Composable
fun MyDialogTitle() {
    Text(text = "title")
}

@Composable
fun MyDialogContent() {
    Text(text = "谢邀,人在美国,刚下飞机")
}


